﻿using System;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

public partial class Ajax_Execute : System.Web.UI.Page
{
    static ClassExecute.ClassExecute dt;
    static string str = webapp4U.Configuration.CurrentConnectionString;
    static string error = "";
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    [WebMethod]
    public static string MoveLevel(decimal TongTien)
    {
        int kq = 0;
        dt = new ClassExecute.ClassExecute();
        kq = dt.ExecuteSQl_int(str, "MoveLevel", CommandType.StoredProcedure, ref error,
            new SqlParameter("@TongTien",TongTien));
        if (kq==1)
        {
            return "Nhiệp vụ chốt thành công";
        }
        else
        {
            return "Nhiệp vụ chốt thất bại";
        }

    }
    [WebMethod]
    public static string CheckAccount(string UserName,string Password)
    {
        string kq = "";
        dt = new ClassExecute.ClassExecute();
        kq = dt.GetDataDepend(str,"CheckAccount", CommandType.StoredProcedure,  ref error,
            new SqlParameter("@UserID", UserName),
            new SqlParameter("@Password",Password));
        if (kq != "Null")
        {
            HttpContext.Current.Session["RoleID"] = kq;
            HttpContext.Current.Session["UserName"] = UserName;
        }
        return kq;

    }
    [WebMethod]
    public static ArrayList GetDepartment()
    {

        ArrayList listDepartment = new ArrayList();
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[GetDepartment]", CommandType.StoredProcedure);
        for (int i = 0; i < db.Rows.Count; i++)
        {
            listDepartment.Add(new { Value = db.Rows[i][0].ToString(), Display = db.Rows[i][1].ToString() });

        }
        return listDepartment;
    }
    [WebMethod]
    public static ArrayList GetNames(string DepartmentID)
    {
        ArrayList listEmp = new ArrayList();
        dt = new ClassExecute.ClassExecute();
        DataTable db1 = dt.Get_Data_DataTable(str, ref error, "[GetFc_employees]", CommandType.StoredProcedure,
            new SqlParameter("@DepartmentID", DepartmentID));

        for (int i = 0; i < db1.Rows.Count; i++)
        {
            listEmp.Add(new { Value = db1.Rows[i][0].ToString(), Display = db1.Rows[i][1].ToString() });

        }
        return listEmp;

    }
    [WebMethod]
    public static ArrayList GetShift()
    {

        ArrayList listShift = new ArrayList();
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_GetShift]", CommandType.StoredProcedure);
        for (int i = 0; i < db.Rows.Count; i++)
        {
            listShift.Add(new { Value = db.Rows[i][0].ToString(), Display = db.Rows[i][1].ToString() });

        }
        return listShift;
    }
    [WebMethod]
    public static string InsertNhanVien(string EmployeeName, string DepartmentID, string RecDate, double Safary,string ShiftID)
    {
        int i = 1;
        dt = new ClassExecute.ClassExecute();
        i = dt.ExecuteSQl_int(str, "[Insert_Fc_Employees]", CommandType.StoredProcedure, ref error,
            new SqlParameter("@EmployeeName", EmployeeName),
            new SqlParameter("@DepartmentID", DepartmentID),
            new SqlParameter("@RecDate", RecDate),
            new SqlParameter("@Salary", Safary),
            new SqlParameter("@ShiftID",ShiftID));
        if (i == 0)
        {
            return "Nghiệp vụ thành công";
        }
        else
        {
            return "Nghiệp vụ thất bại";
        }

    }
    [WebMethod]
    public static string InsertDepartment(string DepartmentName)
    {
        int i = 1;
        dt = new ClassExecute.ClassExecute();
        i = dt.ExecuteSQl_int(str, "[K_InsertDepartment]", CommandType.StoredProcedure, ref error,
            new SqlParameter("@DepartmentName", DepartmentName));
        if (i == 2)
        {
            return "Nghiệp vụ thành công";
        }
        else
        {
            return "Nghiệp vụ thất bại";
        }

    }
    [WebMethod]
    public static string GetTotalLevelCurrent(int level)
    {
        string total = "";
        dt = new ClassExecute.ClassExecute();
        total = dt.GetDataDepend(str, "GetToTalLevel", CommandType.StoredProcedure, ref error,
            new SqlParameter("@level", level));
        return total;
    }
    [WebMethod]
    public ArrayList GET_SALESPNAME(string salesp_key)
    {
        ArrayList Level = new ArrayList();
        dt = new ClassExecute.ClassExecute();
        DataTable db1 = dt.Get_Data_DataTable(str, ref error, "GET_SALESPNAME", CommandType.StoredProcedure,
            new SqlParameter("@salesp_key", salesp_key));
       
        for (int i = 0; i < db1.Rows.Count; i++)
        {
            Level.Add(new { Value = db1.Rows[i][0].ToString(), Display = db1.Rows[i][1].ToString() });

        }
        return Level;

    }
    [WebMethod]
    public static string InsertUngLuong(string EmpID, string Money, int Level)
    {

        bool i = false;
        dt = new ClassExecute.ClassExecute();
        if (Level == 1)
        {
            i = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "[K_UngKyI]", ref error,
               new SqlParameter("@EmployeeID", EmpID),
               new SqlParameter("@Money", Money));
        }
        if (i)
        {
            return "Nghiệp vụ thành công";
        }
        else
        {
            return "Nghiệp vụ thất bại" + error;
        }

    }
    [WebMethod]
    public static string GetLastedUngLuong(string EmpID)
    {
        string lasted = "";
        dt = new ClassExecute.ClassExecute();
        lasted = dt.GetDataDepend(str, "K_GetLatestUngLuong", CommandType.StoredProcedure, ref error,
            new SqlParameter("@EmployeeID", EmpID));
        return lasted;
    }
    [WebMethod]
    public static ArrayList GetMTChan()
    {

        ArrayList listMTChan = new ArrayList();
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_GetMoTaChan]", CommandType.StoredProcedure);
        for (int i = 0; i < db.Rows.Count; i++)
        {
            listMTChan.Add(new { Value = db.Rows[i][0].ToString(), Display = db.Rows[i][1].ToString() });

        }
        return listMTChan;
    }
    [WebMethod]
    public static ArrayList GetMTLe()
    {

        ArrayList listMTLe = new ArrayList();
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "[K_GetMoTaLe]", CommandType.StoredProcedure);
        for (int i = 0; i < db.Rows.Count; i++)
        {
            listMTLe.Add(new { Value = db.Rows[i][0].ToString(), Display = db.Rows[i][1].ToString() });

        }
        return listMTLe;
    }
    [WebMethod]
    public static string InsertProDuct(string ProName, string MoTaChanID, decimal SlChan, string MoTaLeID, decimal SLLe, int CachTinh)
    {
        dt = new ClassExecute.ClassExecute();
        int i = 0;
        i=dt.ExecuteSQl_int(str,"K_InsertProduct",CommandType.StoredProcedure,ref error,
            new SqlParameter("@ProName",ProName),
            new SqlParameter("@MoTaChanID",MoTaChanID),
            new SqlParameter("@SlChan",SlChan),
            new SqlParameter("@MoTaLeID",MoTaLeID),
            new SqlParameter("@SLLe",SLLe),
            new SqlParameter("@CachTinh",CachTinh));
        if (i==1)
        {
            return "Nghiệp  vụ thêm mới thành công";
        }
        else
        {
            return "Nghiệp vụ thêm mới thất bại" + error;
        }
    }

}
